//
//  NewDownloadCell.m
//  Everest
//
//  Created by 李其昌 on 15/1/2.
//  Copyright (c) 2015年 ThreeL. All rights reserved.
//

#import "NewDownloadCell.h"
#import "FileModel.h"
#import "ASIHTTPRequest.h"
#import "NewDownloadViewController.h"
#import "FilesDownManage.h"

#define kCellWidth [UIApplication sharedApplication].keyWindow.bounds.size.width

@implementation NewDownloadCell

- (void)dealloc
{
    [_typeImage release];
    [_request release];
    [_progressView release];
    [_sizePercentage release];
    [_fileImage release];
    [_fileType release];
    [_operateButton release];
    [_fileSize release];
    [_fileCurrentSize release];
    [_fileName release];
    [_fileInfo release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 文件名
        self.fileName = [[UILabel alloc] init];
        self.fileName.text = @"音频名";
        self.fileName.font = [UIFont systemFontOfSize:19];
        [self.contentView addSubview:self.fileName];
        [_fileName release];
        
        // 当前文件大小
        self.fileCurrentSize = [[UILabel alloc] init];
        self.fileCurrentSize.text = @"大小:1024.00k";
        self.fileCurrentSize.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.fileCurrentSize];
        [_fileCurrentSize release];
        
        // 文件总大小
        self.fileSize = [[UILabel alloc] init];
        self.fileSize.text = @"大小:1024.00k";
        self.fileSize.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.fileSize];
        [_fileSize release];
        
        // 文件图片
        self.fileImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.fileImage];
        [_fileImage release];
        
        // 操作按钮
        self.operateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.operateButton setBackgroundImage:[UIImage imageNamed:@"下载管理-开始按钮.png"] forState:UIControlStateNormal];
        [self.operateButton addTarget:self action:@selector(operateTask:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.operateButton];
        
        // 文件类型
        self.fileType = [[UILabel alloc] init];
        self.fileType.text = @"格式:aac";
        self.fileType.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.fileType];
        [_fileType release];
        
        // 文件百分比
        self.sizePercentage = [[UILabel alloc] init];
        self.sizePercentage.text = @"100%";
        self.sizePercentage.font = [UIFont systemFontOfSize:8];
        [self.contentView addSubview:self.sizePercentage];
        [_sizePercentage release];
        
        // 进度条
        self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        [self.contentView addSubview:self.progressView];
        [_progressView release];
        
        // 下载的小图片
        self.typeImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"下载管理下载图标.png"]];
        [self.contentView addSubview:self.typeImage];
        [_typeImage release];
        
        // 删除按钮
        self.deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [self.contentView addSubview:self.deleteButton];
        [self.deleteButton addTarget:self action:@selector(deleteRequest:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return self;
}

- (void)layoutSubviews
{
    self.fileName.frame = CGRectMake(kCellWidth / 10 * 3, self.contentView.frame.size.height * 0.0625, kCellWidth / 2, self.contentView.frame.size.height * 0.375);
    self.fileType.frame = CGRectMake(kCellWidth / 10 * 3, self.contentView.frame.size.height * 0.444, kCellWidth * 0.2, self.contentView.frame.size.height * 0.2);
    self.fileSize.frame = CGRectMake(kCellWidth / 10 * 3 + kCellWidth * 0.2, self.contentView.frame.size.height * 0.444, kCellWidth * 0.3, self.contentView.frame.size.height * 0.25);
    self.progressView.frame = CGRectMake(kCellWidth / 10 * 3, self.contentView.frame.size.height * 0.67, kCellWidth * 0.4, 9);
    self.fileCurrentSize.frame = CGRectMake(kCellWidth / 10 * 3, self.contentView.frame.size.height * 0.74, kCellWidth * 0.51, self.contentView.frame.size.height * 0.26);
    self.fileImage.frame = CGRectMake(kCellWidth * 0.01, self.contentView.frame.size.height * 0.06, kCellWidth * 0.26, self.contentView.frame.size.height * 0.87);
    self.operateButton.frame = CGRectMake(kCellWidth * 0.01, self.contentView.frame.size.height * 0.06, kCellWidth * 0.26, self.contentView.frame.size.height * 0.87);
    self.deleteButton.frame = CGRectMake(kCellWidth * 0.86, self.contentView.frame.size.height * 0.26, kCellWidth * 0.1, self.contentView.frame.size.height * 0.52);
    self.sizePercentage.frame = CGRectMake(kCellWidth * 0.69, self.contentView.frame.size.height * 0.74, kCellWidth * 0.11, self.contentView.frame.size.height * 0.26);
    self.typeImage.frame = CGRectMake(kCellWidth * 0.7, self.contentView.frame.size.height * 0.64, kCellWidth * 0.031, self.contentView.frame.size.height * 0.23);
}

- (void)deleteRequest:(UIButton *)button
{
    FilesDownManage *filedownmanage = [FilesDownManage sharedFilesDownManage];
    [filedownmanage deleteRequest:self.request];
    if ([self.delegate respondsToSelector:@selector(ReloadDownLoadingTable)])
        [((NewDownloadViewController*)self.delegate) ReloadDownLoadingTable];
}

- (void)operateTask:(UIButton *)button
{
    FileModel *downFile = self.fileInfo;
    FilesDownManage *filedownmanage = [FilesDownManage sharedFilesDownManage];
    if(downFile.isDownloading)//文件正在下载，点击之后暂停下载 有可能进入等待状态
    {
        [self.operateButton setBackgroundImage:[UIImage imageNamed:@"下载管理-开始按钮.png"] forState:UIControlStateNormal];
        [filedownmanage stopRequest:self.request];
    }
    else
    {
        [self.operateButton setBackgroundImage:[UIImage imageNamed:@"下载管理-暂停按钮.png"] forState:UIControlStateNormal];
        if (downFile.post) {
        }else
            [filedownmanage resumeRequest:self.request];
    }
    //暂停意味着这个Cell里的ASIHttprequest已被释放，要及时更新table的数据，使最新的ASIHttpreqst控制Cell
    if ([self.delegate respondsToSelector:@selector(ReloadDownLoadingTable)]) {
        [((NewDownloadViewController *)self.delegate) ReloadDownLoadingTable];
    }

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
